import { createModalWithTable } from "./createModal";

// 抓取商品接口
async function fetchGoods() {
    try {
        const res = await fetch("https://jiankang.baidu.com/seller/mall/api/goods/list", {
            method: "POST",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            body: JSON.stringify({ data: { "shopId": "1858059855061663744", "sellerId": "1858059855061663744", "page": 1, "size": 10, "goodsIds": [], "tpCategoryL2Id": "", "startTime": "", "endTime": "", "status": "", "storeId": "173019811961763545" } }),
            credentials: "include" // 自动带 cookie
        });

        const data = await res.json();
        console.log(' 商品 ===============》  ', data)
        createModalWithTable()
        chrome.runtime.sendMessage({ type: "PRODUCT_DATA", data });
    } catch (err) {
        console.error("抓取商品失败", err);
    }
}

// 抓取评价接口
async function fetchEvaluation() {
    try {
        const res = await fetch("https://jiankang.baidu.com/seller/comment/list", {
            method: "POST",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            body: JSON.stringify({ "data": { "shopId": "1858059855061663744", "sellerId": "1858059855061663744", "page": 1, "size": 10, "startTime": "2025-06-19T16:00:00.000Z", "endTime": "2025-08-19T15:59:59.999Z", "defPraise": 10 } }),
            credentials: "include"
        });

        const data = await res.json();
        console.log(' 评价 ===============》  ', data)
        createModalWithTable()
        chrome.runtime.sendMessage({ type: "EVALUATION_DATA", data });
    } catch (err) {
        console.error("抓取评价失败", err);
    }
}

// 监听 popup 指令
chrome.runtime.onMessage.addListener((message) => {
    if (message.type === "FETCH_GOODS") fetchGoods();
    if (message.type === "FETCH_EVALUATION") fetchEvaluation();
});
